<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>考场设置</title>
    <link rel="stylesheet" type="text/css" href="${s.base}/lib/layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="${s.base}/css/base/index.css"/>
    <link rel="stylesheet" type="text/css" href="${s.base}/css/base/theme/theme.css"/>
</head>
<body>
<div class="wrap-container clearfix">
    <div class="column-content-detail">
        <form class="layui-form" action="">
            <div class="tab-step-ctn mart20">
                <div class="tab-step-line ">
                    <span class="circle">1</span>
                    <div class="tab-step-name">基础信息填写</div>
                    <i class="iconfont">&#xe688;</i>
                </div>
                <div class="tab-step-line">
                    <span class="circle">2</span>
                    <div class="tab-step-name">添加考生</div>
                    <i class="iconfont">&#xe688;</i>
                </div>
                <div class="tab-step-line checked">
                    <span class="circle">3</span>
                    <div class="tab-step-name">考场分配</div>
                    <i class="iconfont">&#xe688;</i>
                </div>
                <div class="tab-step-line">
                    <span class="circle">4</span>
                    <div class="tab-step-name">监、巡考老师设置</div>
                    <i class="iconfont">&#xe688;</i>
                </div>
                <div class="tab-step-line">
                    <span class="circle">5</span>
                    <div class="tab-step-name">发布考试通知</div>
                    <i class="iconfont">&#xe688;</i>
                </div>
            </div>
            <div class="kw-mian-ctn">
                <table class="wxyy-item-table" cellspacing="0" cellpadding="0" width="100%" id="showTable">
                    <tbody>
                    <tr>

                        <td id="mytest"><span class="red">*</span>按成绩分配</td>
                        <td class="wxyy-td-tetxleft" id="cjfp">
                            <input type="radio" name="cjfp" value="1" lay-filter="cjfp" title="是" checked>
                            <input type="radio" name="cjfp" value="0" lay-filter="cjfp" title="否">
                        </td>
                        <td class="drcj">导入成绩</td>
                        <td class="drcj w300">
                            <div class="kw-grade-dr">
                                <input type="text" placeholder="请导入成绩" id="studentScore" readonly>
                                <div class="drbtn" id="selectFile">导入</div>
                                <div class="xzmbbtn" id="exportExcel">下载模板</div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td><span class="red">*</span>考场设置</td>
                        <td class="wxyy-td-tetxleft" id="kcsz">
                            <input type="radio" name="kcsz" value="0" title="按考场数" lay-filter="kcsz" checked>
                            <input type="radio" name="kcsz" value="1" title="按考场人数" lay-filter="kcsz">
                        </td>
                        <td class="kcs"><span class="red">*</span>考场数</td>
                        <td><input class=" wxyy-tab-input" id="kcNum"></td>
                    </tr>
                    </tbody>
                    <tbody id="tbodyid">
                    </tbody>
                </table>

                <div class="layui-form-item ">
                    <div class="layui-input-block" style="margin-left: 0px !important;text-align: center; margin-top: 30px !important;">
                        <button class="layui-btn layui-btn-normal wxyy-tab-btn" id="frontStep">上一步</button>
                        <button class="layui-btn layui-btn-normal wxyy-tab-btn" id="nextStep">下一步</button>
                        <button type="button" id="cancel" class="layui-btn layui-btn-primary wxyy-tab-btn">取消</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="${s.base}/lib/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script src="${s.base}/js/common.js" type="text/javascript" charset="utf-8"></script>
<script>
    var tableIns = null;
    var applyId = '${applyId}';
    var ksId = '${ksId}';
    var id = "";
    var mStudentNum = 0;
    var mKwKcMessageVOList = null;
    var newcjfp = "1";
    var newkcfp = "1";
    var menuId='${parentMenuId}';

    layui.use(['jquery', 'page', 'dialog', 'baseutil', 'form','upload'], function () {
        var $ = layui.jquery,
            page = layui.page,
            dialog = layui.dialog,
            form = layui.form,
            upload = layui.upload,
            baseutil = layui.baseutil;

        baseutil.post('/work/kwmanagement/kwmanagement/addOrUpdateStop', {"step": "3", "id": ksId}, function (result) {
            if (0 == result.code) {
            }
        });
        postData();

        //是否成绩分配的监听事件
        form.on('radio(cjfp)', function (data) {
            debugger
            var value = this.value;
            newcjfp = value;
            showCjfpNew();

        })
        //是否按考场分配的监听事件
        form.on('radio(kcsz)', function (data) {
            var value = this.value;
            newkcfp = value;
            showKcNum()

        })

        //考场数/考场人数失去焦点事件
        $("#kcNum").blur(function () {
            showKcInfo();
        });

        //取消
        $("#cancel").click(function () {
            parent.tabDelete('kwKcSettingEdit');

        });

        //上一步 跳转添加学生界面
        $("#frontStep").click(function () {
            page.edit("getNewEdit", "/work/kwmanagement/kwstudentmessage/getNewEdit?applyId=" + applyId + "&ksId=" + ksId+"&parentMenuId="+menuId, "添加学生");
            parent.tabDelete('kwKcSettingEdit');
        });

        //上传学生成绩
        upload.render({
            elem: '#selectFile'
            ,url: '/work/kwmanagement/kwstudentmessage/importStudnetScoreExcel'
            , accept: 'file'
            , data: {ksId:ksId,applyId:applyId}
            , multiple: true
            , auto: true
            ,before:function(obj){
                layer.load();
            }
            ,done:function(res){
                curr=1;
                layer.closeAll('loading');
                if (res.code === 0) {
                    //界面显示数据
                    $("#studentScore").val(res.msg);
                    dialog.msg("操作成功");
                } else {
                    dialog.msg("操作失败");
                }
            }
        });
        //导出学生成绩模板
        $("#exportExcel").on("click",function () {
            window.location.href = "/work/kwmanagement/kwkcsetting/exportStudentScore?applyId="+applyId+"&ksId="+ksId;
        });

        //下一步保存考场信息
        $("#nextStep").click(function () {
            //更新步骤
            var cjfp = $(":radio[name='cjfp']:checked").val();
            if (cjfp == "1") {
                var String = $("#studentScore").val();
                if(String =="")
                {
                    dialog.msg("请导入成绩");
                    return false;
                }
            }
            var kcsz = $(":radio[name='kcsz']:checked").val();
            if ($("#kcNum").val() == "") {
                if (kcsz == "0") {
                    dialog.msg("请输入考场数");
                } else if (kcsz == "1") {
                    dialog.msg("请输入考场人数");
                }
                return false;
            } else {
                var str = $("#kcNum").val();
                if (kcsz == "1") {
                    if (!/^[1-9]\d*$/.test(str)) {
                        dialog.msg("考场数输入不合法");
                        return false;
                    }
                } else if (kcsz == "2") {
                    if (!/^[1-9]\d*$/.test(str)) {
                        dialog.msg("考场人数输入不合法");
                        return false;
                    }
                }
            }

            var data = {};
            if (!isBlank(id)) {
                data.id = id;
            }
            data.ksId = ksId;
            data.cjfp = $(":radio[name='cjfp']:checked").val();
            data.kcsz = $(":radio[name='kcsz']:checked").val();
            data.kcNum = $("#kcNum").val();
            data.applyId = applyId;
            var kwKcMessageJson = [];
            var tr = $("#showTable tbody#tbodyid tr");
            var sum = 0;
            for (var i = 0; i < tr.length; i++) {
                var kcNum = tr[i].childNodes[0].innerHTML.substring(2, tr[i].childNodes[0].innerHTML.length - 2);
                var kcStu = tr[i].childNodes[1].childNodes[0].value;
                if (kcStu != null && kcStu != "") {
                    if (!/^[1-9]\d*$/.test(kcStu)) {
                        dialog.msg("考场" + kcNum + "人数输入不合法");
                        return false;
                    }
                } else {
                    dialog.msg("请输入考场" + kcNum + "人数");
                    return false;
                }
                sum += parseInt(kcStu);
                var kcPlace = tr[i].childNodes[3].childNodes[0].value;
                kwKcMessageJson.push({ksId: ksId, kcNum: kcNum, kcStu: kcStu, kcPlace: kcPlace});
            }
            if (sum != mStudentNum) {
                dialog.msg("参加考试" + mStudentNum + "人，已分配" + sum + "人");
                return false;
            }
            data.kwKcMessageJson = JSON.stringify(kwKcMessageJson);
            baseutil.post('/work/kwmanagement/kwkcsetting/addOrUpdate', data, function (result) {
                if (0 === result.code) {
                    parent.tabRefresh(menuId);
                    page.edit("kwKcMessageListEdit", "/work/kwmanagement/kwKcMessageListEdit?applyId=" + applyId + "&ksId=" + ksId+"&parentMenuId="+menuId, "设置监巡考老师");
                    parent.tabDelete('kwKcSettingEdit');
                    //刷新上一个界面的操作
                }
            });
            return false;
        });



        //成绩分配
        function showCjfpNew() {
            var value = $(":radio[name='cjfp']:checked").val();
                if(value=="0"){
                    //console.log(value)
                    $("#cjfp").attr("colspan","3");
                    $(".drcj").hide();
                }else{
                    $("#cjfp").attr("colspan","1");
                    $(".drcj").show();
                }

        }
        //考场分配
        function showKcNum() {
            var kcsz = $(":radio[name='kcsz']:checked").val();
            if (kcsz == "0") {
                $(".kcs").html("考场数")
            } else {
                $(this).parent("td").parent("tr").siblings(".kc").remove();
                $(".kcs").html("考场人数")
            }
            showKcInfo();
        }

        //设置考场信息
        function showKcInfo() {
            if (mStudentNum == 0) {
                baseutil.post('/work/kwmanagement/kwstudentmessage/staticStudent', {"ksId": ksId, "applyId": applyId}, function (result) {
                    if (0 == result.code) {
                        mStudentNum = result.data.length;
                        showKcInfoDetail();
                    }
                });
            } else {
                showKcInfoDetail();
            }
        }

        //获取考务考场基本信息
        function postData() {
            baseutil.post('/work/kwmanagement/kwkcsetting/findSettingOneVo', {"ksId": ksId, "applyId": applyId}, function (result) {
                if (result.code == 0 && result.data != null) {
                    if (isBlank(id)) {
                        id = result.data.id;
                    }
                    newcjfp = result.data.cjfp;
                    newkcfp = result.data.kcsz;

                    $("input:radio[name='cjfp'][value= '" + result.data.cjfp + "']").attr("checked", true);
                    $("input:radio[name='kcsz'][value= '" + result.data.kcsz + "']").attr("checked", true);

                    form.render();

                    $("#kcNum").val(result.data.kcNum);
                    mKwKcMessageVOList = result.data.kwKcMessageVOList;
                } else {
                    $("input:radio[name='cjfp'][value= '1']").attr("checked", true);
                    form.render();
                }
                showCjfpNew()
                showKcNum();
            });
        }

    });

    //考场详情界面
    function showKcInfoDetail() {
        var str = '';
        if ($("#kcNum").val() == "" || $("#kcNum").val() == null) {
        } else {
            var kcsz = $(":radio[name='kcsz']:checked").val();
            var kcNum = 0;
            var num = new Array();
            if (kcsz == "0") {
                kcNum = parseInt($("#kcNum").val());
                var everyNum = parseInt(mStudentNum / kcNum);
                var duoyuNum = mStudentNum % kcNum;
                var num = new Array();
                for (var i = 0; i < kcNum; i++) {
                    num[i] = everyNum;
                }
                for (var i = 0; i < parseInt(duoyuNum); i++) {
                    num[i] += 1;
                }
            }
            else if (kcsz == "1") {
                var everyNum = parseInt($("#kcNum").val());
                kcNum = parseInt(mStudentNum / everyNum);
                var duoyuNum = mStudentNum - (everyNum * kcNum);
                var num = new Array();
                for (var i = 0; i < kcNum; i++) {
                    num[i] = everyNum;
                }
                if (duoyuNum != 0) {
                    num[i] = duoyuNum;
                    kcNum++;
                }
            }
            if (mKwKcMessageVOList != null && mKwKcMessageVOList.length == kcNum) {
                for (var i = 0; i < kcNum; i++) {
                    str += '<tr>';
                    str += '<td>考场' + (i + 1) + '人数</td>';
                    str += '<td><input class=" wxyy-tab-input" placeholder="请输入考场人数" value="' + num[i] + '"></td>';
                    str += '<td>考场' + (i + 1) + '地点</td>';
                    str += '<td><input class=" wxyy-tab-input" placeholder="请输入考场地点" value="' + mKwKcMessageVOList[i].kcPlace + '"></td>';
                    str += '</tr>';
                }
            } else {
                for (var i = 0; i < kcNum; i++) {
                    str += '<tr>';
                    str += '<td>考场' + (i + 1) + '人数</td>';
                    str += '<td><input class=" wxyy-tab-input" placeholder="请输入考场人数" value="' + num[i] + '"></td>';
                    str += '<td>考场' + (i + 1) + '地点</td>';
                    str += '<td><input class=" wxyy-tab-input" placeholder="请输入考场地点"></td>';
                    str += '</tr>';
                }
            }
            $("#showTable tbody#tbodyid").html(str);
        }

    }


</script>
</body>
</html>